<?php
session_start();
include 'common/check.php';
include 'common/json.php';
if ((isset($_SESSION['upload'])) && (trim($_SESSION['upload']) != "") && (trim($_SESSION['upload'])!= "[]")) {
    $uploadList = $_SESSION['upload'];
}
if ($_GET['action'] == "delete") {
    $len = count($uploadList);
    for ($i = 0; $i < $len; $i++) {
        if ($uploadList[$i]['id'] == $_GET['id']) {
           unset($uploadList[$i]);
        }
    }
} else {
    if (isset($_GET['link'])) {
        $uploadList[] = array("id"=>time(), "content"=>$_GET['link']);
    } else {
        include 'common/smarty.php';
        //定义允许上传的MIME格式
        define("UPLOAD_IMAGE_MIME", "image/pjpeg,image/jpg,image/jpeg,image/gif,image/x-png,image/png");
        //图片允许大小，字节
        define("UPLOAD_IMAGE_SIZE", 102400);
        //图片大小用KB为单位来表示
        define("UPLOAD_IMAGE_SIZE_KB", 100);
        //图片上传的路径
        define("UPLOAD_IMAGE_PATH", "upload/");
        //获取允许的图像格式
        $mime = explode(",", UPLOAD_IMAGE_PATH);
        $is_vaild = 1;
        $uploadMsg = "";
        //遍历所有允许格式
        /*foreach ($mime as $type) {
            if ($_FILES['file']['type'] == $type) {
                $is_valid = 1;
            }
        }*/
        //如果格式正确，并且没有超过大小就上传上去
        $fileName = explode(".", $_FILES['file']['name']);
        $fileName = $_SESSION['userID'] . time() . "." . $fileName[1];
        if ($is_vaild && $_FILES['file']['size'] <= UPLOAD_IMAGE_SIZE && $_FILES['file']['size'] > 0) {
           if (move_uploaded_file($_FILES['file']['tmp_name'], UPLOAD_IMAGE_PATH . $fileName)) {
               $uploadList[] = array("id"=>time(), "content"=>$fileName);
            } else {
                $uploadMsg = "Upload failed";
            }
        } else {
            $uploadMsg = "Upload failed: Type is not right or too large to upload.";
        }
        $_SESSION['upload'] = $uploadList;
        $json = new Services_JSON();
        if ($uploadMsg != "") {
            $jsonList = $json->encode($uploadList);
?>
    <script language="javascript" type="text/javascript">
        window.parent.fillTable("1");
    </script>
<?php
        }
        exit;
    }
}
$_SESSION['upload'] = $uploadList;
$json = new Services_JSON();
echo (count($uploadList) == 0 ? null : $json->encode($uploadList));
?>
